//
//  SimGesturePopNavVC.m
//  Pods
//
//  Created by LiuXubin on 16/3/10.
//
//

#import "SimGesturePopNavVC.h"
#import "UIViewController+SimDragNavVC.h"

@interface SimGesturePopNavVC () <UIGestureRecognizerDelegate, UINavigationControllerDelegate>

@end

@implementation SimGesturePopNavVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        NSArray *targets = [self.interactivePopGestureRecognizer valueForKey:@"_targets"];
        if ([targets isKindOfClass:[NSArray class]]) {
            @try {
                id interactivePanTarget = [[targets firstObject] valueForKey:@"target"];
                _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:interactivePanTarget action:NSSelectorFromString(@"handleNavigationTransition:")];
                _panRecognizer.delegate = self;
                [self.view addGestureRecognizer:_panRecognizer];
                self.interactivePopGestureRecognizer.enabled = NO;
                self.interactivePopGestureRecognizer.delegate = self;
                [self.interactivePopGestureRecognizer requireGestureRecognizerToFail:_panRecognizer];
//                [self.view removeGestureRecognizer:self.interactivePopGestureRecognizer];
            }
            @catch (NSException *exception) {
                NSLog(@"%@", exception);
            }
        }
    }
}

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.enabled = NO;
        _panRecognizer.enabled = NO;
    }
    
    return [super popToRootViewControllerAnimated:animated];
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.enabled = NO;
        _panRecognizer.enabled = NO;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.enabled = NO;
        _panRecognizer.enabled = NO;
    }
    
    [super setViewControllers:viewControllers animated:animated];
}

#pragma mark -  UINavigationControllerDelegate 
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.enabled = NO;
        _panRecognizer.enabled = YES;
        return;
    }
}

#pragma mark - UIGestureRecognizer
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if(self.viewControllers.count <= 1){
        return NO;
    }
    
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint velocity = [panGesture velocityInView:panGesture.view];
        if (velocity.x < 0) {
            return NO;
        }
        
        double radian = atan(velocity.y/velocity.x);
        double degree = radian * 180 / M_PI;
        if (fabs(degree) > 45) {
            return NO;
        }
    }
    
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (_panRecognizer == gestureRecognizer) {
        return [self.topViewController shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
    }
    return NO;
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (_panRecognizer == gestureRecognizer) {
        return [self.topViewController shouldBeginDragGesture:gestureRecognizer receiveTouch:touch];
    }
    
    return YES;
}

@end
